﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;

namespace iLangl.BusinessModel.DAL.XMLStorage
{
	public class ProductDAL : BaseStorageDAL, IProductDAL
	{


		#region IProductDAL Members

		public void AddProduct(Model.Product product, System.Data.IDbTransaction trans)
		{
			dtoXmlStorage.ProductRow productRow = Storage.Product.FindById(product.Id);

			bool isNew = (productRow == null);

			if (isNew)
			{
				productRow = Storage.Product.NewProductRow();
			}

			productRow.Description = product.Description;
			productRow.Id = product.Id;
			productRow.IsApproved = product.IsApproved;
			productRow.Name = product.Name;

			if (isNew)
			{
				Storage.Product.AddProductRow(productRow);
			}

			if (trans == null)
			{
				AcceptChanges();
			}
		}

		public void AddTPProduct(Model.TPProduct product, System.Data.IDbTransaction trans)
		{
			dtoXmlStorage.TPProductRow tpproductrow = Storage.TPProduct.FindByTPIdTPProductId(product.TpId, product.TPProductId);

			bool isNew = (tpproductrow == null);

			if (isNew)
			{
				tpproductrow = Storage.TPProduct.NewTPProductRow();
			}

			tpproductrow.IsApproved = product.IsApproved;
			tpproductrow.ProductId = product.Id;
			tpproductrow.TPId = product.TpId;
			tpproductrow.TPProductDescription = product.TPProductDescription;
			tpproductrow.TPProductId = product.TPProductId;
			tpproductrow.TPProductName = product.TPProductName;
			

			if (isNew)
			{
				Storage.TPProduct.AddTPProductRow(tpproductrow);
			}

			if (trans == null)
			{
				AcceptChanges();
			}
		}

		public Product ReadProductById(string id)
		{
			dtoXmlStorage.ProductRow productRow = Storage.Product.FindById(id);

			if (productRow == null)
				return null;

			Product result = new Product();
			result.Id = productRow.Id;

			if (!productRow.IsNameNull())
				result.Name = productRow.Name;

			if(!productRow.IsDescriptionNull())
				result.Description = productRow.Description;

			if (!productRow.IsIsApprovedNull())
				result.IsApproved = productRow.IsApproved;

			return result;
		}

		public Model.Product ReadProductByName(string name)
		{
			foreach (dtoXmlStorage.ProductRow pr in Storage.Product)
			{
				if (string.Compare(name, pr.Name, true) == 0)
				{
					return ReadProductById(pr.Id);
				}
			}

			return null;

		}


		public Product ReadTpProduct(string tpId, string Id)
		{
			foreach(dtoXmlStorage.TPProductRow productRow in Storage.TPProduct)
			{
				if (
					(String.Compare(productRow.TPId, tpId, true) == 0)
					&& (String.Compare(productRow.ProductId, Id) == 0))
				{
					Product product = new Product();

					product.Id = productRow.TPProductId;
					
					if (!productRow.IsTPProductNameNull()) 
						product.Name = productRow.TPProductName;

					if (!productRow.IsTPProductDescriptionNull()) 
						product.Description = productRow.TPProductDescription;

					return product;
				}
			}

			return null;
		}

		public IList<Product> ReadProducts()
		{
			IList<Product> result = new List<Product>();
			foreach (dtoXmlStorage.ProductRow pr in Storage.Product)
			{
				result.Add(ReadProductById(pr.Id));
			}
			return result;
		}

		#endregion
	}
}
